<?php
/*
* Author: Nguyen Hai Nam
* Module: Admin
* Desc	: Introduce pick.vn and sidle pic
* Date	: 12/12/2011
*/
class LoginModule extends Pick_Core_Module{
	
	public $authentication = 0;
	
	function LoginModule(){
		
		parent::Pick_Core_Module();
		$this->theme = "admin";
		# Languages
		$languages = new Pick_Core_Languages();
		$objectLanguages = "";
		$objectLanguages = $languages->getObjects(1, 'status = 1', array('position'=>'ASC'), '');
		if($objectLanguages){
			$objectLanguages = $objectLanguages['data'];	
		}#endif
		$this->template->assign("objectLanguages", $objectLanguages);		
        
        // Get Path for this module
        $this->template->assign('arrayParentPath', $this->getArrayParentPath());
	}
	public function getContent(){
		$act = $this->request->element('act', "login");
		$this->template->assign('act', $act);
		if($act == "login"){
			return $this->loginAction();
		}else{
			return $this->listAction();	
		}
	}
	public function loginAction(){
		$error ='';
		if($this->request->isPost()){
			$username = trim($this->request->element("username"));
			$password = trim($this->request->element("password"));
			$users = new Pick_Core_User();
			$userId = $users->authenticateUser($username,$password);

			if($userId) {
	
				$_SESSION['userId'] 	= $userId;
				$_SESSION['userType'] 	= $users->getUserType($userId);
				$_SESSION['userName'] 	= $username;
				
				redirect(base_url()."/admin");
				
			} else {	
				$_SESSION['userId'] = 0;	
				$error = $this->l("invalid_user_password");
				$this->template->assign('act', 'login');
			}
			$this->template->assign('username',$username);	
		}
		$this->template->assign('error',$error);
	}
}